var express = require('express');
var jwt = require('jsonwebtoken')
var constant = require('../../../private/constant')
var db = require('../../../tools/database')
var util = require('../../../tools/util')
var router = express.Router();

// 登录, 签发 token
// 错误处理标准化 √√
router.use((req, res, next)=>{
    sql = `SELECT * FROM Users WHERE NetID= ? AND UserKey= ?`;
    db.queryCallback(sql, [req.body.userID, req.body.key], res, (result) => {
        util.dataRespond(signToken(result.UserID, result.NetID, result.UserName), res)
    }, true)
});

function signToken(userID, netID, userName){
    return jwt.sign({
        algorithm: 'HS256',
        subject: 'normal',
        issuer: 'Reina',
        userID: userID,
        netID: netID,
        userName: userName
        }, constant.secretKey,
        {expiresIn: constant.expiresIn}
    )
}

module.exports = router;